# coding:utf-8
# @Time     : 2018/9/26 下午3:20
# @Author   : 程杨
# @Discribe : 上线单条用户测试
import datetime
import numpy as np
import time
import pandas as pd
import traceback
from model.Config import Dept_app
from common.logger import Logger



class Applist_ContactExtractor():
    def __init__(self):
        self._apply_id = None
        self.all_dept_app = Dept_app().dept_applist
        self.logger =  Logger("applist_score.log").getLogger()

    # 获取用户每次上传数据时距离上次上传的app变化状况
    def get_app_diff(self, applist_list):
        installed_list = []
        installed_num_list = []
        del_list = []
        del_num_list = []

        for i in range(len(applist_list)):
            if i == 0:
                app_lst_last = ''
                app_lst_this = ''
            else:
                app_lst_last = applist_list[i - 1].strip().split(' ')
                app_lst_this = applist_list[i].strip().split(' ')
            date_ins = [app for app in app_lst_this if app not in app_lst_last]
            date_del = [app for app in app_lst_last if app not in app_lst_this]
            installed_list.append(' '.join(date_ins))
            installed_num_list.append(len(date_ins))
            del_list.append(' '.join(date_del))
            del_num_list.append(len(date_del))
        return installed_list, installed_num_list, del_list, del_num_list

    # 计算applist中的借贷类app
    def get_dept_applist(self, applist):
        dept_app_list = []
        for app in applist.strip().split(' '):
            if app in self.all_dept_app:
                dept_app_list.append(app)
        return ' '.join(dept_app_list)

    # 转换时间戳为时间
    def time_transform(self, time_stamp):
        return time.strftime("%Y%m%d%H%M%S", time.localtime(int(time_stamp / 1000)))

    # 计算两个时间相差的天数
    def get_time_diff(self, a, b):
        diff = (datetime.datetime(int(str(int(a))[0:4]), int(str(int(a))[4:6]), int(str(int(a))[6:8])) -
                datetime.datetime(int(str(int(b))[0:4]), int(str(int(b))[4:6]), int(str(int(b))[6:8]))).days
        return diff

    # 求两个集合的交集
    def get_repeat(self, set1, set2):
        return [e for e in set1 if e in set2]

    def get_appname(self,info_lst):
        app_list  = []
        for app_info in info_lst:
            app = app_info['package_name']
            app_list.append(app)
        return ' '.join(app_list)

    def get_applist_feature(self, apply_time, data):
        from collections import OrderedDict
        user_dict = OrderedDict()
        user_dict['apply_time'] = int(apply_time)
        data = pd.DataFrame(data)
        data['app_all'] = data['applist'].apply(lambda x:self.get_appname(x))
        data = data.rename(columns={'date': 'datekey'})
        data = data.sort_values('datekey')
        data['datekey'] = data['datekey'].astype(int)
        user_date_df = data.copy()
        # 用户每天的app变化状况
        user_date_df['app_num'] = user_date_df['app_all'].apply(
            lambda x: 0 if x.strip() == '' else len(x.strip().split(' ')))
        installed_list, installed_num_list, del_list, del_num_list = self.get_app_diff(user_date_df['app_all'])
        user_date_df['installed'] = installed_list
        user_date_df['installed_num'] = installed_num_list
        user_date_df['deleted'] = del_list
        user_date_df['deleted_num'] = del_num_list
        # 用户每天属于借贷类的app和数量
        user_date_df['dept_app'] = user_date_df['app_all'].apply(lambda x: self.get_dept_applist(x))
        user_date_df['dept_app_num'] = user_date_df['dept_app'].apply(
            lambda x: 0 if x.strip() == '' else len(x.strip().split(' ')))
        # 用户每天安装/删除借贷类的app和数量
        user_date_df['installed_dept_app'] = user_date_df['installed'].apply(lambda x: self.get_dept_applist(x))
        user_date_df['installed_dept_app_num'] = user_date_df['installed_dept_app'].apply(
            lambda x: 0 if x.strip() == '' else len(x.strip().split(' ')))
        user_date_df['deleted_dept_app'] = user_date_df['deleted'].apply(lambda x: self.get_dept_applist(x))
        user_date_df['deleted_dept_app_num'] = user_date_df['deleted_dept_app'].apply(
            lambda x: 0 if x.strip() == '' else len(x.strip().split(' ')))
        # 提取用户特征
        # 用户applist最大/最小/平均数量
        user_dict['max_size'] = np.max(user_date_df['app_num'])
        user_dict['min_size'] = np.min(user_date_df['app_num'])
        user_dict['avg_size'] = np.average(user_date_df['app_num'])
        # 用户借贷类applist最大/最小/平均值
        user_dict['max_dept_size'] = np.max(user_date_df['dept_app_num'])
        user_dict['min_dept_size'] = np.min(user_date_df['dept_app_num'])
        user_dict['avg_depy_size'] = np.average(user_date_df['dept_app_num'])
        # 用户原始applist数量
        user_dict['ori_size'] = np.max(
            user_date_df[user_date_df['datekey'] == np.min(user_date_df['datekey'])]['app_num'])
        # 用户原始借贷类applist数量
        user_dict['ori_dept_size'] = np.max(
            user_date_df[user_date_df['datekey'] == np.min(user_date_df['datekey'])]['dept_app_num'])
        # 用户原始借贷类applist占比
        if user_dict['ori_size'] > 0:
            user_dict['ori_dept_rate'] = user_dict['ori_dept_size'] / user_dict['ori_size']
        else:
            user_dict['ori_dept_rate'] = 0
        # 用户最近一次的applist数量
        user_dict['latest_size'] = np.max(user_date_df[user_date_df['datekey'] == np.max(
            user_date_df[user_date_df['datekey'] <= user_dict['apply_time']]['datekey'])]['app_num'])
        # 用户最近一次的借贷类applist数量
        user_dict['latest_dept_size'] = np.max(user_date_df[user_date_df['datekey'] == np.max(
            user_date_df[user_date_df['datekey'] <= user_dict['apply_time']]['datekey'])]['dept_app_num'])
        # 用户最近一次借贷类applist占比
        if user_dict['latest_size'] > 0:
            user_dict['latest_dept_rate'] = user_dict['latest_dept_size'] / user_dict['latest_size']
        else:
            user_dict['latest_dept_rate'] = 0
        # 用户安装applist总数量
        user_dict['installed_size'] = sum(user_date_df['installed_num'])
        # 用户安装applist平均数
        user_dict['installed_avg_size'] = np.average(user_date_df['installed_num'])
        # 用户安装借贷类applist总数量
        user_dict['installed_dept_size'] = sum(user_date_df['installed_dept_app_num'])
        # 用户安装借贷类applist平均数
        user_dict['installed_dept_avg_size'] = np.average(user_date_df['installed_dept_app_num'])
        # 用户安装借贷类applist的总比例
        if user_dict['installed_size'] > 0:
            user_dict['installed_dept_rate'] = user_dict['installed_dept_size'] / user_dict['installed_size']
        else:
            user_dict['installed_dept_rate'] = 0
        # 用户删除applist总数量
        user_dict['deleted_size'] = sum(user_date_df['deleted_num'])
        # 用户删除applist平均数
        user_dict['deleted_avg_size'] = np.average(user_date_df['deleted_num'])
        # 用户删除借贷类applist总数量
        user_dict['deleted_dept_size'] = sum(user_date_df['deleted_dept_app_num'])
        # 用户删除借贷类applist平均数
        user_dict['deleted_dept_avg_size'] = np.average(user_date_df['deleted_dept_app_num'])
        # 用户删除借贷类applist的总比例
        if user_dict['deleted_size'] > 0:
            user_dict['deleted_dept_rate'] = user_dict['deleted_dept_size'] / user_dict['deleted_size']
        else:
            user_dict['deleted_dept_rate'] = 0
        # 用户所有存在过的applist
        existed_set = set()
        for app_str in user_date_df['app_all']:
            app_lst = app_str.strip().split(' ')
            for app in app_lst:
                if app != '':
                    existed_set.add(app)
        # 用户所有存在过的applist无重复的总数
        user_dict['installed_distinct_size'] = len(existed_set)
        # 用户所有存在过的借贷类applist无重复的总数
        existed_dept_set = set()
        for app in self.get_dept_applist(' '.join(existed_set)).split(' '):
            if app != '':
                existed_dept_set.add(app)
        user_dict['installed_distinct_dept_size'] = len(existed_dept_set)
        # 用户所有存在过的借贷类applist无重复的比例
        if user_dict['installed_distinct_size'] > 0:
            user_dict['installed_distinct_dept_rate'] = user_dict['installed_distinct_dept_size'] / user_dict[
                'installed_distinct_size']
        else:
            user_dict['installed_distinct_dept_rate'] = 0
        # 用户所有删除过的applist
        deleted_set = set()
        for app_str in user_date_df['deleted']:
            app_lst = app_str.strip().split(' ')
            for app in app_lst:
                if app != '':
                    deleted_set.add(app)
        # 用户所有删除过的applist无重复的总数
        user_dict['deleted_distinct_size'] = len(deleted_set)
        # 用户所有删除过的借贷类applist无重复的总数
        deleted_dept_set = set()
        for app in self.get_dept_applist(' '.join(deleted_set)).split(' '):
            if app != '':
                deleted_dept_set.add(app)
        user_dict['deleted_distinct_dept_size'] = len(deleted_dept_set)
        # 用户所有删除过的借贷类applist无重复的比例
        if user_dict['deleted_distinct_size'] > 0:
            user_dict['deleted_distinct_dept_rate'] = user_dict['deleted_distinct_dept_size'] / user_dict[
                'deleted_distinct_size']
        else:
            user_dict['deleted_distinct_dept_rate'] = 0
        # 用户一次性安装applist的最大数量
        user_dict['once_installed_max_size'] = np.max(user_date_df['installed_num'])
        # 用户一次性安装借贷类applist的最大数量
        user_dict['once_installed_dept_max_size'] = np.max(user_date_df['installed_dept_app_num'])
        # 用户一次性安装借贷类applist最大时的占比
        if user_date_df.loc[user_date_df['installed_dept_app_num'].idxmax()]['installed_num'] > 0:
            user_dict['once_installed_dept_max_rate'] = np.max(user_date_df['installed_dept_app_num']) \
                                                        / user_date_df.loc[
                                                            user_date_df['installed_dept_app_num'].idxmax()][
                                                            'installed_num']
        else:
            user_dict['once_installed_dept_max_rate'] = 0
        # 用户一次性安装借贷类applist最大时距离申请的时间
        if np.max(user_date_df['installed_dept_app_num']) == 0:
            user_dict['once_installed_dept_max_from_submit'] = self.get_time_diff(user_dict['apply_time'],
                                                                             np.min(user_date_df['datekey']))
        else:
            user_dict['once_installed_dept_max_from_submit'] = self.get_time_diff(user_dict['apply_time'],
                                                                             user_date_df.loc[user_date_df[
                                                                                 'installed_dept_app_num'].idxmax()][
                                                                                 'datekey'])
        # 用户一次性删除applist的最大数量
        user_dict['once_deleted_max_size'] = np.max(user_date_df['deleted_num'])
        # 用户一次性删除借贷类applist的最大数量
        user_dict['once_deleted_dept_max_size'] = np.max(user_date_df['deleted_dept_app_num'])
        # 用户一次性删除借贷类applist最大时的占比
        if user_date_df.loc[user_date_df['deleted_dept_app_num'].idxmax()]['deleted_num'] > 0:
            user_dict['once_deleted_dept_max_rate'] = np.max(user_date_df['deleted_dept_app_num']) \
                                                      / user_date_df.loc[user_date_df['deleted_dept_app_num'].idxmax()][
                                                          'deleted_num']
        else:
            user_dict['once_deleted_dept_max_rate'] = 0
        # 用户一次性删除借贷类applist最大时距离申请的时间
        if np.max(user_date_df['deleted_dept_app_num']) == 0:
            user_dict['once_deleted_dept_max_from_submit'] = self.get_time_diff(user_dict['apply_time'],
                                                                           np.min(user_date_df['datekey']))
        else:
            user_dict['once_deleted_dept_max_from_submit'] = self.get_time_diff(user_dict['apply_time'],
                                                                           user_date_df.loc[user_date_df[
                                                                               'deleted_dept_app_num'].idxmax()][
                                                                               'datekey'])
        # 时序特征
        # 用户最近一次安装applist数量
        user_dict['latest_installed_size'] = np.max(user_date_df[user_date_df['datekey'] == np.max(
            user_date_df[user_date_df['datekey'] <= user_dict['apply_time']]['datekey'])]['installed_num'])
        # 用户最近一次安装借贷类applist数量
        user_dict['latest_installed_dept_size'] = np.max(user_date_df[user_date_df['datekey'] == np.max(
            user_date_df[user_date_df['datekey'] <= user_dict['apply_time']]['datekey'])]['installed_dept_app_num'])
        # 用户最近一次安装借贷类applist占比
        if user_dict['latest_installed_size'] > 0:
            user_dict['latest_installed_dept_rate'] = user_dict['latest_installed_dept_size'] / user_dict[
                'latest_installed_size']
        else:
            user_dict['latest_installed_dept_rate'] = 0
        # 用户最近一次删除applist数量
        user_dict['latest_deleted_size'] = np.max(user_date_df[user_date_df['datekey'] == np.max(
            user_date_df[user_date_df['datekey'] <= user_dict['apply_time']]['datekey'])]['deleted_num'])
        # 用户最近一次删除借贷类applist数量
        user_dict['latest_deleted_dept_size'] = np.max(user_date_df[user_date_df['datekey'] == np.max(
            user_date_df[user_date_df['datekey'] <= user_dict['apply_time']]['datekey'])]['deleted_dept_app_num'])
        # 用户最近一次删除借贷类applist占比
        if user_dict['latest_deleted_size'] > 0:
            user_dict['latest_deleted_dept_rate'] = user_dict['latest_deleted_dept_size'] / user_dict[
                'latest_deleted_size']
        else:
            user_dict['latest_deleted_dept_rate'] = 0
        # 生成时序特征所需要的时间
        # 距离申请三天的时间
        time_3_day = int(datetime.datetime.strftime(datetime.datetime.strptime(str(user_dict['apply_time']), '%Y%m%d') - \
                                                    datetime.timedelta(days=3), '%Y%m%d'))
        # 距离申请一周的时间
        time_7_day = int(datetime.datetime.strftime(datetime.datetime.strptime(str(user_dict['apply_time']), '%Y%m%d') - \
                                                    datetime.timedelta(days=7), '%Y%m%d'))
        # 距离申请一月的时间
        time_30_day = int(
            datetime.datetime.strftime(datetime.datetime.strptime(str(user_dict['apply_time']), '%Y%m%d') - \
                                       datetime.timedelta(days=30), '%Y%m%d'))
        # 用户最近三天安装applist数量
        target_df = user_date_df[(user_date_df['datekey'] <= user_dict['apply_time']) & \
                                 (user_date_df['datekey'] >= time_3_day)]
        user_dict['recent_3_installed_size'] = sum(target_df['installed_num'])
        # 用户最近三天安装借贷类applist数量
        user_dict['recent_3_installed_dept_size'] = sum(target_df['installed_dept_app_num'])
        # 用户最近三天安装借贷类applist占比
        if user_dict['recent_3_installed_size'] > 0:
            user_dict['recent_3_installed_dept_rate'] = user_dict['recent_3_installed_dept_size'] / user_dict[
                'recent_3_installed_size']
        else:
            user_dict['recent_3_installed_dept_rate'] = 0
        # 用户最近三天删除applist数量
        user_dict['recent_3_deleted_size'] = sum(target_df['deleted_num'])
        # 用户最近三天删除借贷类applist数量
        user_dict['recent_3_deleted_dept_size'] = sum(target_df['deleted_dept_app_num'])
        # 用户最近三天删除借贷类applist占比
        if user_dict['recent_3_deleted_size'] > 0:
            user_dict['recent_3_deleted_dept_rate'] = user_dict['recent_3_deleted_dept_size'] / user_dict[
                'recent_3_deleted_size']
        else:
            user_dict['recent_3_deleted_dept_rate'] = 0
        # 用户最近一周安装applist数量
        target_df = user_date_df[(user_date_df['datekey'] <= user_dict['apply_time']) & \
                                 (user_date_df['datekey'] >= time_7_day)]
        user_dict['recent_7_installed_size'] = sum(target_df['installed_num'])
        # 用户最近一周安装借贷类applist数量
        user_dict['recent_7_installed_dept_size'] = sum(target_df['installed_dept_app_num'])
        # 用户最近一周安装借贷类applist占比
        if user_dict['recent_7_installed_size'] > 0:
            user_dict['recent_7_installed_dept_rate'] = user_dict['recent_7_installed_dept_size'] / user_dict[
                'recent_7_installed_size']
        else:
            user_dict['recent_7_installed_dept_rate'] = 0
        # 用户最近一周删除applist数量
        user_dict['recent_7_deleted_size'] = sum(target_df['deleted_num'])
        # 用户最近一周删除借贷类applist数量
        user_dict['recent_7_deleted_dept_size'] = sum(target_df['deleted_dept_app_num'])
        # 用户最近一周删除借贷类applist占比
        if user_dict['recent_7_deleted_size'] > 0:
            user_dict['recent_7_deleted_dept_rate'] = user_dict['recent_7_deleted_dept_size'] / user_dict[
                'recent_7_deleted_size']
        else:
            user_dict['recent_7_deleted_dept_rate'] = 0
        # 用户最近一月安装applist数量
        target_df = user_date_df[(user_date_df['datekey'] <= user_dict['apply_time']) & \
                                 (user_date_df['datekey'] >= time_30_day)]
        user_dict['recent_30_installed_size'] = sum(target_df['installed_num'])
        # 用户最近一月安装借贷类applist数量
        user_dict['recent_30_installed_dept_size'] = sum(target_df['installed_dept_app_num'])
        # 用户最近一月安装借贷类applist占比
        if user_dict['recent_30_installed_size'] > 0:
            user_dict['recent_30_installed_dept_rate'] = user_dict['recent_30_installed_dept_size'] / user_dict[
                'recent_30_installed_size']
        else:
            user_dict['recent_30_installed_dept_rate'] = 0
        # 用户最近一月删除applist数量
        user_dict['recent_30_deleted_size'] = sum(target_df['deleted_num'])
        # 用户最近一月删除借贷类applist数量
        user_dict['recent_30_deleted_dept_size'] = sum(target_df['deleted_dept_app_num'])
        # 用户最近一月删除借贷类applist占比
        if user_dict['recent_30_deleted_size'] > 0:
            user_dict['recent_30_deleted_dept_rate'] = user_dict['recent_30_deleted_dept_size'] / user_dict[
                'recent_30_deleted_size']
        else:
            user_dict['recent_30_deleted_dept_rate'] = 0
        # 反复安装和删除的app特征
        installed_frequency = OrderedDict()
        for app_str in (user_date_df['installed'].append(user_date_df['app_all'].head(1))):
            app_lst = app_str.strip().split(' ')
            for app in app_lst:
                if app in installed_frequency.keys() and app != '':
                    installed_frequency[app] += 1
                else:
                    installed_frequency[app] = 1
        # 用户最近一次的applist中，安装次数>1的applist数量
        latest_repeat_installed_size = 0
        target_set = set()
        for line in list(user_date_df.tail(1)['installed']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if installed_frequency[app] > 1:
                latest_repeat_installed_size += 1
        user_dict['latest_repeat_installed_size'] = latest_repeat_installed_size
        # 用户最近一次的applist中，安装次数>1的借贷类applist数量
        latest_repeat_installed_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(1)['installed_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest_repeat_installed_dept_size += 1
        user_dict['latest_repeat_installed_dept_size'] = latest_repeat_installed_dept_size
        # 用户最近一次的applist中，安装次数>1的借贷类applist占比
        if user_dict['latest_repeat_installed_size'] > 0:
            user_dict['latest_repeat_installed_dept_rate'] = user_dict['latest_repeat_installed_dept_size'] / user_dict[
                'latest_repeat_installed_size']
        else:
            user_dict['latest_repeat_installed_dept_rate'] = 0

        # 用户最近3天的applist中，安装次数>1的applist数量
        latest3_repeat_installed_size = 0
        target_set = set()
        for line in list(user_date_df.tail(3)['installed']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest3_repeat_installed_size += 1
        user_dict['latest3_repeat_installed_size'] = latest3_repeat_installed_size
        # 用户最近3天的applist中，安装次数>1的借贷类applist数量
        latest3_repeat_installed_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(3)['installed_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest3_repeat_installed_dept_size += 1
        user_dict['latest3_repeat_installed_dept_size'] = latest3_repeat_installed_dept_size
        # 用户最近3天的applist中，安装次数>1的借贷类applist占比
        if user_dict['latest3_repeat_installed_size'] > 0:
            user_dict['latest3_repeat_installed_dept_rate'] = user_dict['latest3_repeat_installed_dept_size'] / \
                                                              user_dict['latest3_repeat_installed_size']
        else:
            user_dict['latest3_repeat_installed_dept_rate'] = 0

        # 用户最近一周的applist中，安装次数>1的applist数量
        latest7_repeat_installed_size = 0
        target_set = set()
        for line in list(user_date_df.tail(7)['installed']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest7_repeat_installed_size += 1
        user_dict['latest7_repeat_installed_size'] = latest7_repeat_installed_size
        # 用户最近一周的applist中，安装次数>1的借贷类applist数量
        latest7_repeat_installed_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(7)['installed_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest7_repeat_installed_dept_size += 1
        user_dict['latest7_repeat_installed_dept_size'] = latest7_repeat_installed_dept_size
        # 用户最近一周的applist中，安装次数>1的借贷类applist占比
        if user_dict['latest7_repeat_installed_size'] > 0:
            user_dict['latest7_repeat_installed_dept_rate'] = user_dict['latest7_repeat_installed_dept_size'] / \
                                                              user_dict['latest7_repeat_installed_size']
        else:
            user_dict['latest7_repeat_installed_dept_rate'] = 0

        # 用户最近一月的applist中，安装次数>1的applist数量
        latest30_repeat_installed_size = 0
        target_set = set()
        for line in list(user_date_df.tail(30)['installed']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest30_repeat_installed_size += 1
        user_dict['latest30_repeat_installed_size'] = latest30_repeat_installed_size
        # 用户最近一月的applist中，安装次数>1的借贷类applist数量
        latest30_repeat_installed_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(30)['installed_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if installed_frequency[app] > 1:
                    latest30_repeat_installed_dept_size += 1
        user_dict['latest30_repeat_installed_dept_size'] = latest30_repeat_installed_dept_size
        # 用户最近一月的applist中，安装次数>1的借贷类applist占比
        if user_dict['latest30_repeat_installed_size'] > 0:
            user_dict['latest30_repeat_installed_dept_rate'] = user_dict['latest30_repeat_installed_dept_size'] / \
                                                               user_dict['latest30_repeat_installed_size']
        else:
            user_dict['latest30_repeat_installed_dept_rate'] = 0
        deleted_frequency = OrderedDict()
        for app_str in (user_date_df['deleted']):
            app_lst = app_str.strip().split(' ')
            for app in app_lst:
                if app in deleted_frequency.keys() and app != '':
                    deleted_frequency[app] += 1
                else:
                    deleted_frequency[app] = 1
        # 用户最近一次的applist中，删除次数>1的applist数量
        latest_repeat_deleted_size = 0
        target_set = set()
        for line in list(user_date_df.tail(1)['deleted']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest_repeat_deleted_size += 1
        user_dict['latest_repeat_deleted_size'] = latest_repeat_deleted_size
        # 用户最近一次的applist中，删除次数>1的借贷类applist数量
        latest_repeat_deleted_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(1)['deleted_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest_repeat_deleted_dept_size += 1
        user_dict['latest_repeat_deleted_dept_size'] = latest_repeat_deleted_dept_size
        # 用户最近一次的applist中，删除次数>1的借贷类applist占比
        if user_dict['latest_repeat_deleted_size'] > 0:
            user_dict['latest_repeat_deleted_dept_rate'] = user_dict['latest_repeat_deleted_dept_size'] / user_dict[
                'latest_repeat_deleted_size']
        else:
            user_dict['latest_repeat_deleted_dept_rate'] = 0

        # 用户最近3天的applist中，删除次数>1的applist数量
        latest3_repeat_deleted_size = 0
        target_set = set()
        for line in list(user_date_df.tail(3)['deleted']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest3_repeat_deleted_size += 1
        user_dict['latest3_repeat_deleted_size'] = latest3_repeat_deleted_size
        # 用户最近3天的applist中，删除次数>1的借贷类applist数量
        latest3_repeat_deleted_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(3)['deleted_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest3_repeat_deleted_dept_size += 1
        user_dict['latest3_repeat_deleted_dept_size'] = latest3_repeat_deleted_dept_size
        # 用户最近3天的applist中，删除次数>1的借贷类applist占比
        if user_dict['latest3_repeat_deleted_size'] > 0:
            user_dict['latest3_repeat_deleted_dept_rate'] = user_dict['latest3_repeat_deleted_dept_size'] / user_dict[
                'latest3_repeat_deleted_size']
        else:
            user_dict['latest3_repeat_deleted_dept_rate'] = 0

        # 用户最近一周的applist中，删除次数>1的applist数量
        latest7_repeat_deleted_size = 0
        target_set = set()
        for line in list(user_date_df.tail(7)['deleted']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest7_repeat_deleted_size += 1
        user_dict['latest7_repeat_deleted_size'] = latest7_repeat_deleted_size
        # 用户最近一周的applist中，删除次数>1的借贷类applist数量
        latest7_repeat_deleted_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(7)['deleted_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest7_repeat_deleted_dept_size += 1
        user_dict['latest7_repeat_deleted_dept_size'] = latest7_repeat_deleted_dept_size
        # 用户最近一周的applist中，删除次数>1的借贷类applist占比
        if user_dict['latest7_repeat_deleted_size'] > 0:
            user_dict['latest7_repeat_deleted_dept_rate'] = user_dict['latest7_repeat_deleted_dept_size'] / user_dict[
                'latest7_repeat_deleted_size']
        else:
            user_dict['latest7_repeat_deleted_dept_rate'] = 0

        # 用户最近一月的applist中，删除次数>1的applist数量
        latest30_repeat_deleted_size = 0
        target_set = set()
        for line in list(user_date_df.tail(30)['deleted']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest30_repeat_deleted_size += 1
        user_dict['latest30_repeat_deleted_size'] = latest30_repeat_deleted_size
        # 用户最近一月的applist中，删除次数>1的借贷类applist数量
        latest30_repeat_deleted_dept_size = 0
        target_set = set()
        for line in list(user_date_df.tail(30)['deleted_dept_app']):
            app_lst = line.strip().split(' ')
            for app in app_lst:
                if app != '':
                    target_set.add(app)
        for app in target_set:
            if app != '':
                if deleted_frequency[app] > 1:
                    latest30_repeat_deleted_dept_size += 1
        user_dict['latest30_repeat_deleted_dept_size'] = latest30_repeat_deleted_dept_size
        # 用户最近一月的applist中，删除次数>1的借贷类applist占比
        if user_dict['latest30_repeat_deleted_size'] > 0:
            user_dict['latest30_repeat_deleted_dept_rate'] = user_dict['latest30_repeat_deleted_dept_size'] / user_dict[
                'latest30_repeat_deleted_size']
        else:
            user_dict['latest30_repeat_deleted_dept_rate'] = 0
        # 用户借贷类app打分：一个借贷类app存在一天+1分/总天数
        if len(user_date_df) > 0:
            user_dict['dept_score'] = sum(user_date_df['dept_app_num']) / len(user_date_df)
        else:
            user_dict['dept_score'] = 0

        del user_dict['apply_time']
        return user_dict



    '''
    抽取applist变量
    :param input_json:
    :return:
    '''
    def parse(self, apply_time, applist_data, applist_model):
        from collections import OrderedDict
        res = OrderedDict()
        values = OrderedDict()
        if len(applist_data) == 0:
            res['app_score'] = 1000
            self.logger.info('empty applist data')
            return res
        try:
            values.update(self.get_applist_feature(apply_time, applist_data))
            df = pd.DataFrame([values])
            score = round(applist_model.predict_proba(df)[0][1] * 1000, 2)
        except Exception as e:
            err_message = traceback.format_exc(limit=1)
            self.logger.info("appid:" + str(self._apply_id) + ' ' + err_message)
            self._apply_id = None
            res['app_score'] = 1000
            return res
        res['app_score'] = score
        return res



